package com.dream.xiaobo.service.impl;

import com.dream.xiaobo.dao.impl.CartDaoImpl;
import com.dream.xiaobo.dao.impl.OrdersDaoImpl;
import com.dream.xiaobo.entity.Cart;
import com.dream.xiaobo.entity.Item;
import com.dream.xiaobo.entity.Orders;
import com.dream.xiaobo.service.OrdersService;
import com.dream.xiaobo.utils.RandomUtils;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @author xiaobo
 * @date 2021/9/2 - 15:08
 */
public class OrdersServiceImpl implements OrdersService {

    //创建订单Dao实例化对象
    private OrdersDaoImpl ordersDao = new OrdersDaoImpl();

    //创建购物车实例化对象
    private CartDaoImpl cartDao = new CartDaoImpl();

    /**
     * 创建订单业务逻辑接口实现方法
     * @param uId
     * @param sum
     * @param aId
     * @throws IllegalAccessException
     * @throws SQLException
     * @throws InvocationTargetException
     */
    @Override
    public void createOrder(String uId, String sum, String aId) throws IllegalAccessException, SQLException, InvocationTargetException {

        //类型转换、将总金额转换为BigDeciml类型
        BigDecimal bigSum = new BigDecimal(sum);
        //创建订单实例化对象
        Orders orders = new Orders();
        //调用工具类生成订单Id
        String oId = RandomUtils.createOrderId();
        //依次赋值
        orders.setoId(oId);
        orders.setoCount(bigSum);
        orders.setaId(Integer.parseInt(aId));
        orders.setoState(1);
        orders.setuId(Integer.parseInt(uId));
        orders.setoTime(new Date());
        //调用dao方法
        ordersDao.insertOrder(orders);
        //调用dao方法
        List<Cart> carts = cartDao.selectCartByUid(Integer.parseInt(uId));
        //创建订单项集合实例化对象
        List<Item> items = new ArrayList<>();
        //遍历购物车
        for (Cart cart : carts) {
            //创建订单项实例化对象
            Item item = new Item();
            //依次赋值
            item.setoId(oId);
            item.setiNum(cart.getcNum());
            item.setiCount(cart.getcCount());
            item.setpId(cart.getpId());
            //将订单项对象存放到集合中
            items.add(item);
        }

        //调用dao方法
        ordersDao.insertItem(items);
    }

    /**
     * 查询订单信息业务逻辑接口实现方法（根据用户Id）
     * @param uId
     * @return
     * @throws IllegalAccessException
     * @throws SQLException
     * @throws InvocationTargetException
     * @throws ParseException
     */
    @Override
    public List<Orders> findOrderByUId(Integer uId) throws IllegalAccessException, SQLException, InvocationTargetException, ParseException {

        //调用dao接口
        List<Orders> list = ordersDao.selectOrderByUId(uId);

        //返回查询到的信息
        return list;
    }

    /**
     * 查询订单信息业务逻辑接口实现方法（根据订单Id）
     * @param oId
     * @return
     * @throws IllegalAccessException
     * @throws SQLException
     * @throws InvocationTargetException
     */
    @Override
    public Orders findOrderByOId(String oId) throws IllegalAccessException, SQLException, InvocationTargetException {

        //调用dao方法
        Orders orders = ordersDao.selectOrderByOId(oId);

        //调用dao方法
        List<Item> items = ordersDao.selectItemByOId(oId);
        //将订单项放到订单中
        orders.setItems(items);

        //返回查询到的信息
        return orders;
    }

    /**
     * 更新订单业务逻辑接口实现方法（根据订单Id）
     * @param oId
     * @throws SQLException
     */
    @Override
    public void updateOrderByOId(String oId) throws SQLException {

        //调用dao方法
        ordersDao.updateOrderByOId(oId);

    }

    /**
     * 查询订单业务逻辑接口实现方法
     * @return
     * @throws SQLException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @Override
    public List<Orders> findOrder() throws SQLException, InvocationTargetException, IllegalAccessException {

        //调用dao接口
        List<Orders> list = ordersDao.selectOrder();

        //返回查询到的集合
        return list;
    }

    /**
     * 更新订单业务逻辑接口实现方法（发货）
     * @param oId
     * @throws SQLException
     */
    @Override
    public void updateOrderByOIdHair(String oId) throws SQLException {
        //调用dao方法
        ordersDao.updateOrderByOidHair(oId);

    }

    /**
     * 更新订单业务逻辑接口实现方法（收货）
     * @param oId
     * @throws SQLException
     */
    @Override
    public void updateORderByOIdCollect(String oId) throws SQLException {
        //调用dao方法
        ordersDao.updateOrderByOIdCollect(oId);
    }
}